package com.bishe.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.entity.Finance;
import com.bishe.mapper.FinanceMapper;
import com.bishe.service.FinanceService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description 针对表【finance】的数据库操作Service实现
 * @createDate 2024-03-07 14:46:10
 */
@Service
public class FinanceServiceImpl extends ServiceImpl<FinanceMapper, Finance>
        implements FinanceService {

    private final static List<String> monthList = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");


    @Override
    public List<Object> nowYearData(String id) {
        List<Finance> userBills = list(
                Wrappers.lambdaQuery(Finance.class)
                        .eq(Finance::getUid, id)
                        .between(Finance::getCreateTime,
                                DateUtil.format(DateUtil.beginOfYear(new Date()), DatePattern.NORM_DATE_PATTERN),
                                DateUtil.format(DateUtil.endOfYear(new Date()), DatePattern.NORM_DATE_PATTERN)));


        List<Finance> arrayList = new ArrayList<>();
        for (String mon : monthList) {
            if (CollUtil.isEmpty(userBills)) {
                if (Integer.parseInt(mon) < 10) {
                    mon = "0" + mon;
                }
                Finance data = new Finance();
                data.setId(IdUtil.fastSimpleUUID());
                data.setCreateTime(DateUtil.parse(DateUtil.thisYear() + "-" + mon + "-01", DatePattern.NORM_DATE_PATTERN));
                data.setIncome(0.0);
                data.setPay(0.0);
                data.setBudget(0.0);
                arrayList.add(data);
            } else {
                Map<String, List<Finance>> collect = userBills.stream().collect(Collectors.groupingBy(s -> String.valueOf(DateUtil.month(s.getCreateTime()))));
                Finance data = new Finance();
                if (!collect.containsKey(mon)) {
                    data.setCreateTime(DateUtil.parse(DateUtil.thisYear() + "-" + mon + "-01", DatePattern.NORM_DATE_PATTERN));
                    data.setIncome(0.0);
                    data.setPay(0.0);
                    data.setBudget(0.0);
                    arrayList.add(data);
                } else {
                    List<Finance> finances = collect.get(mon);
                    double Budget = finances.stream().filter(Objects::nonNull).mapToDouble(Finance::getBudget).sum();
                    double Income = finances.stream().filter(Objects::nonNull).mapToDouble(Finance::getIncome).sum();
                    double Pay = finances.stream().filter(Objects::nonNull).mapToDouble(Finance::getPay).sum();
                    data.setIncome(Income);
                    data.setPay(Pay);
                    data.setBudget(Budget);
                    data.setCreateTime(DateUtil.parse(DateUtil.thisYear() + "-" + mon + "-01", DatePattern.NORM_DATE_PATTERN));
                    arrayList.add(data);
                }
            }
        }

        Map<String, Object> resultMap = new HashMap<>();

        // 收入
        List<Double> Income = arrayList.stream()
                .sorted(Comparator.comparing(Finance::getCreateTime).reversed())
                .map(Finance::getIncome)
                .collect(Collectors.toList());
        Collections.reverse(Income);

        resultMap.put("name", "收入");
        resultMap.put("type", "bar");
        resultMap.put("data", Income);


        Map<String, Object> resultMap2 = new HashMap<>();
        // 支出
        List<Double> pay = arrayList.stream()
                .sorted(Comparator.comparing(Finance::getCreateTime).reversed())
                .map(Finance::getPay)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        Collections.reverse(pay);

        resultMap2.put("name", "支出");
        resultMap2.put("type", "bar");
        resultMap2.put("data", pay);

        Map<String, Object> resultMap3 = new HashMap<>();
        //预算
        List<Double> budget = arrayList.stream()
                .sorted(Comparator.comparing(Finance::getCreateTime).reversed())
                .map(Finance::getIncome)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        Collections.reverse(budget);

        resultMap3.put("name", "预算");
        resultMap3.put("type", "bar");
        resultMap3.put("data", budget);

        List<Object> result = new ArrayList<>();
        result.add(resultMap);
        result.add(resultMap2);
        result.add(resultMap3);

        return result;
    }

    @Override
    public List<?> selectById(String id) {
        return list(Wrappers.lambdaQuery(Finance.class).eq(Finance::getUid, id).orderByDesc(Finance::getCreateTime));
    }
}




